var imsiApi = module.exports.imsiApi = require('./core-util').coreApi
var imsiSortMapFn = function(key,scend){
  return {
    order:key,
    orderModel:scend>0?'asc':'desc'
  }
}
module.exports.createGridOrder = function(initKey,initScend){
  var key = initKey
    , scend = initScend
  var getOrder = function getOrder(){
    return imsiSortMapFn(key,scend)
  }
  getOrder.setNewOrder = function(k,s){
    key = k;
    scend = s;
  }
  return getOrder
}
module.exports.parseToPartIMSIGrid = function(data,params){
  return {
    data:data,
    sortKey:params.order,
    sortScend:params.orderModel==='asc'?1:-1,
    hasMore:data.length === params.limit,
    showMore:function(){
      return imsiApi(params,{
        offset:this.data.length
      }).fetch().then(function(innerData,innerParams){
        return {
          data:innerData,
          hasMore:innerData.length === innerParams.limit
        }
      })
    }
  }
}
